/*
 * Copyright 2016 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.xcx.common.dto.base;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.SerializationUtils;

import java.io.Serializable;
import java.lang.reflect.Type;

/**
 * 类BaseDto.java的实现描述：基类
 *
 * @author AngleSuper.Wei
 */
public abstract class BaseDto implements Serializable, Cloneable {

    /**
     * 序列号
     */
    private static final long serialVersionUID = 7169376241894952680L;

    /**
     * JSON格式化工具
     */
    protected static final Gson GSON = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd HH:mm:ss")
            .serializeNulls()
            .addSerializationExclusionStrategy(
                    new BaseJsonParse())
            .addDeserializationExclusionStrategy(
                    new BaseJsonParse()).create();

    /**
     * (non-Javadoc)
     *
     * @see Object#toString()
     */
    @Override
    public String toString() {
        return GSON.toJson(this);
    }

    /**
     * (non-Javadoc)
     *
     * @see Object#clone()
     */
    @Override
    public Object clone() {
        return SerializationUtils.clone(this);
    }

    /**
     * Object to JSON String
     *
     * @param object
     * @return
     */
    public static String toString(Object object) {
        return GSON.toJson(object);
    }

    /**
     * JSON String to Object
     *
     * @param json
     * @param classOfT
     * @return
     */
    public static <T> T fromJson(String json, Class<T> classOfT) {
        return GSON.fromJson(json, classOfT);
    }

    /**
     * JSON String to Object
     *
     * @param json
     * @param typeOfT
     * @return
     */
    public static <T> T fromJson(String json, Type typeOfT) {
        return GSON.fromJson(json, typeOfT);
    }
}
